import urls from '/util/urls'
import request from '/util/request'
import { alert } from '/util/utils'

const map = {
  statsRoutineWork: 'statsRoutineWork',
  A01: 'statsEnvWork',
  A02: 'statsNetworkWork',
  A03: 'statsSecurityWork',
  A04: 'statsServerWork',
  A05: 'statsStorageWork',
  A06: 'statsAssetWork',
  A07: 'statsDocumentWork',
  A08: 'statsAssistWork',
  A09: 'statsIncrementWork',
  statsInsuranceWork: 'statsInsuranceWork'
}

Page({
  data: {
    form: {
      id: '',
      questionnaireId: '',
      preface: '',
      statsRoutineWork: 0,
      statsEnvWork: 0,
      statsNetworkWork: 0,
      statsSecurityWork: 0,
      statsServerWork: 0,
      statsStorageWork: 0,
      statsAssetWork: 0,
      statsDocumentWork: 0,
      statsAssistWork: 0,
      statsIncrementWork: 0,
      statsInsuranceWork: 0,
      excellence: '',
      experience: '',
      target: '',
      content: ''
    },
    questionnaireType: ''
  },
  onLoad(query) {
    this.data.form.questionnaireId = query.taskId
    request.login(this.init.bind(this))
  },
  init() {
    const that = this
    request.get(urls.questionnaireDetailUrl(this.data.form.questionnaireId), data => {
      dd.setNavigationBar({
        title: data.title
      })
      that.setData({
        questionnaireType: data.type
      })
      that.getFeedbackInfo(data)
    })
  },
  getFeedbackInfo(taskInfo) {
    const that = this
    request.get(urls.feedbackByTaskIdUrl(taskInfo.id), info => {
      if (info) {
        that.setData({
          form: info
        })
      } else {
        that.getServiceSummaryOfWorkerUrl(taskInfo.year, taskInfo.type)
      }
    })
  },
  getServiceSummaryOfWorkerUrl(year, type) {
    const that = this
    request.get(urls.getServiceSummaryOfWorkerUrl(year, type), data => {
      data.forEach(v => {
        that.setData({
          ['form.' + map[v['service_type_code']]]: v.times,
        });
      })
    })
  },
  onItemInput(e) {
    this.setData({
      [e.target.dataset.field]: e.detail.value,
    });
  },
  validData(formData) {
    if (!formData.preface) {
      alert('请填写工作序言')
      return false
    }
    if (!formData.excellence) {
      alert('请填写工作亮点')
      return false
    }
    if (!formData.experience) {
      alert('请填写工作感想')
      return false
    }
    if (!formData.target) {
      alert('请填写目标及计划')
      return false
    }
    if (!formData.content) {
      alert('请填写总结')
      return false
    }
    return true
  },
  onSave() {
    const that = this
    const formData = this.data.form
    if (!that.validData(formData)) {
      return
    }
    dd.showLoading({
      content: '保存中...',
    })
    if (formData.id !== '') {
      request.put(urls.updateFeedbackUrl(formData.id), formData, () => {
        that.saveSuccess()
      }, () => {
        that.submitFailure()
      })
    } else {
      request.post(urls.createFeedbackUrl(), formData, () => {
        that.saveSuccess()
      }, () => {
        that.submitFailure()
      })
    }
  },
  onSubmit() {
    const that = this
    const formData = this.data.form
    if (!that.validData(formData)) {
      return
    }
    dd.showLoading({
      content: '提交中...',
    })
    if (formData.id !== '') {
      request.put(urls.updateFeedbackUrl(formData.id), formData, () => {
        request.put(urls.submitFeedbackUrl(formData.id), {}, () => {
          that.submitSuccess()
        }, () => {
          that.submitFailure()
        })
      }, () => {
        that.submitFailure()
      })
    } else {
      request.post(urls.createFeedbackUrl(), formData, (response) => {
        request.put(urls.submitFeedbackUrl(response.id), {}, () => {
          that.submitSuccess()
        }, () => {
          that.submitFailure()
        })
      }, () => {
        that.submitFailure()
      })
    }
  },
  saveSuccess() {
    dd.hideLoading()
    dd.showToast({
      type: 'success',
      content: '保存成功',
      duration: 2000,
      success: () => {
        dd.redirectTo({
          url: '/pages/questionnaire/questionnaire'
        })
      },
    })
  },
  submitSuccess() {
    dd.hideLoading()
    dd.showToast({
      type: 'success',
      content: '提交成功',
      duration: 2000,
      success: () => {
        dd.redirectTo({
          url: '/pages/questionnaire/questionnaire'
        })
      },
    })
  },
  submitFailure() {
    dd.hideLoading()
    dd.showToast({
      type: 'fail',
      content: '操作失败',
      duration: 2000,
    })
  }
});
